
#利用python实现一个多线程程序
#将多线程程序改为多进程程序

import requests
from threading import Thread
from multiprocessing import Process
import time

#模拟请求哔哩哔哩
def request_blibli(index):


    url = "https://www.bilibili.com/"
    headers = {
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit / 537.36 (KHTML, like Gecko) Chrome / 87.0.4280.88 Safari / 537.36"
    }
    response = requests.get(url = url, headers = headers)
    print(response)

#实现一个多线程程序
#等待任务完成回到主进程
if __name__ == "__main__":
    thread_array = []
    start = time.time()

    for i in range(10):

        t = Thread(target = request_blibli, args = (i,))
        thread_array.append(t)
        t.start()

    for t in thread_array:
        t.join()

    print("done")

    end = time.time()
    print(f"cpu执行时间: {end - start}")

#将多线程改为多进程程序
    Process_array = []
    start = time.time()

    for i in range(10):
        p = Process(target = request_blibli, args = (i,))
        Process_array.append(p)
        p.start()

    for p in thread_array:
        p.join()

    print("done")
    end = time.time()
    print(f"cpu执行时间: {end - start}")
